<?php
namespace App\Http\Controllers\Webapi;
use App\Common\ApiData;
use App\Http\Controllers\Controller;
use App\Models\V1\CompanyModel;
use App\Models\V1\DrpModel;
use Illuminate\Http\Request;
use DB;


class CompanyController extends Controller{

    function __construct(){
        $this->apiData  = new ApiData();
    }

    //获得公司详细信息
    public function company_info(Request $request){

        $companyId      = $request->input('companyId');
        $companyModel   = new CompanyModel();
        $companyInfo    = $companyModel->get_company_info($companyId);

        $empDrp              = new \stdClass();
        $empDrp->YourName    = "无";
        $empDrp->FilePath1   = change_member_head_img('');


        $drpModel       = new DrpModel();
        $parentUserId   = $companyInfo->parent_user_id;
        $parentDrpInfo  = $empDrp;
        $grandParentDrpInfo = $empDrp;

        if($parentUserId > 0){
            $drpInfo  = $drpModel->get_drp_info_by_user_id($parentUserId,true);
            if($drpInfo){
                $parentDrpInfo = $drpInfo;
            }

            if($parentDrpInfo && $parentDrpInfo->ParentID){
                $drpInfo = $drpModel->get_drp_info_by_drpid($parentDrpInfo->ParentID,true);
                if($drpInfo){
                    $grandParentDrpInfo = $drpInfo;
                }
            }
        }

        return $this->apiData
            ->set_data('companyInfo',$companyInfo)
            ->set_data('parentDrpInfo',$parentDrpInfo)
            ->set_data('grandParentDrpInfo',$grandParentDrpInfo)
            ->send(200,'ok');

    }



    /**
     * 公司详情
     *
     */
    public function company_detail(Request $request)
    {
        $companyId      = $request->input('companyId');
        $companyModel   = new CompanyModel();
        $companyInfo    = $companyModel->get_company_info($companyId);

        //检查公司是否超出4个月

        $beginTime       = strtotime($companyInfo->vip_begin_date);
        $companyInfo->newVipEndTime = date('Y-m-d',strtotime("+1 year"));

        //算出套餐已使用的时间
        $monthNum   = 1;
        while(true)
        {
            $reduceTime = "-".$monthNum." month";
            $preMonth   = strtotime($reduceTime);
            if($preMonth - $beginTime > 0)
            {
                $monthNum++;
            }else{
                break;
            }
        };


        //找到最近一次购买套餐的信息

        $oldPackageInfo = $companyModel->get_company_last_upgrade($companyId);

        $companyInfo->company_package_id    = $oldPackageInfo->company_package_id;
        $companyInfo->total_money           = $oldPackageInfo->money;
        $companyInfo->usedMonth             = $monthNum;

        return apiData()->set_data('companyInfo',$companyInfo)->send(200,'ok');

    }

}